﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using C1.Phone;
using C1.Phone.RichTextBox;
using C1.Phone.RichTextBox.Documents;

namespace ControlExplorer.Controls.RichTextBox
{
    public partial class FormatPopup : UserControl
    {
        public C1RichTextBox RichTextBox
        {
            get;
            set;
        }

        public C1Popup Popup
        {
            get;
            set;
        }

        // defined colors
        private SolidColorBrush _highlightColor1 = new SolidColorBrush(Colors.Yellow);
        private SolidColorBrush _highlightColor2 = new SolidColorBrush(Color.FromArgb(255, 0, 255, 0)); // lime
        private SolidColorBrush _highlightColor3 = new SolidColorBrush(Colors.Orange);
        private SolidColorBrush _fontColor1 = new SolidColorBrush(Colors.Blue);
        private SolidColorBrush _fontColor2 = new SolidColorBrush(Colors.Green);
        private SolidColorBrush _fontColor3 = new SolidColorBrush(Colors.Red);

        public FormatPopup()
        {
            InitializeComponent();

            // init color buttons
            btnHighlight1.Background = _highlightColor1;
            btnHighlight2.Background = _highlightColor2;
            btnHighlight3.Background = _highlightColor3;
            btnFontColor1.Background = _fontColor1;
            btnFontColor2.Background = _fontColor2;
            btnFontColor3.Background = _fontColor3;

            Loaded += (s, e) =>
            {
                InitializeButtons();
            };

        }

        private void ClosePopup()
        {
            if (Popup != null)
            {
                // close C1Popup
                Popup.IsOpen = false;
            }
        }

        private void InitializeButtons()
        {
            if (RichTextBox == null)
            {
                throw new ArgumentException("RichTextBox property is null");
            }
            // checks which buttons should be toggled
            btnBold.IsChecked = RichTextBox.Selection.FontWeight == FontWeights.Bold ? true : false;
            btnItalic.IsChecked = RichTextBox.Selection.FontStyle == FontStyles.Italic ? true : false;
            btnUnderline.IsChecked = RichTextBox.Selection.TextDecorations == C1TextDecorations.Underline ? true : false;
            btnStrike.IsChecked = RichTextBox.Selection.TextDecorations == C1TextDecorations.Strikethrough ? true : false;
            btnSubscript.IsChecked = RichTextBox.Selection.InlineAlignment == C1VerticalAlignment.Sub ? true : false;
            btnSuperscript.IsChecked = RichTextBox.Selection.InlineAlignment == C1VerticalAlignment.Super ? true : false;

            btnHighlight1.IsChecked = RichTextBox.Selection.InlineBackground == _highlightColor1 ? true : false;
            btnHighlight2.IsChecked = RichTextBox.Selection.InlineBackground == _highlightColor2 ? true : false;
            btnHighlight3.IsChecked = RichTextBox.Selection.InlineBackground == _highlightColor3 ? true : false;

            btnFontColor1.IsChecked = RichTextBox.Selection.Foreground == _fontColor1 ? true : false;
            btnFontColor2.IsChecked = RichTextBox.Selection.Foreground == _fontColor2 ? true : false;
            btnFontColor3.IsChecked = RichTextBox.Selection.Foreground == _fontColor3 ? true : false;

            btnAlignLeft.IsChecked = RichTextBox.Selection.TextAlignment == C1TextAlignment.Left ? true : false;
            btnAlignCenter.IsChecked = RichTextBox.Selection.TextAlignment == C1TextAlignment.Center ? true : false;
            btnAlignRight.IsChecked = RichTextBox.Selection.TextAlignment == C1TextAlignment.Right ? true : false;

            if (RichTextBox.Selection.List != null)
            {
                btnBullets.IsChecked = RichTextBox.Selection.List.MarkerStyle == TextMarkerStyle.Disc ? true : false;
                btnNumbering.IsChecked = RichTextBox.Selection.List.MarkerStyle == TextMarkerStyle.Decimal ? true : false;
            }
            else
            {
                btnBullets.IsChecked = false;
                btnNumbering.IsChecked = false;
            }
        }

        #region format
        private void btnBold_Click(object sender, RoutedEventArgs e)
        {
            // bold
            if ((bool)btnBold.IsChecked)
            {
                RichTextBox.Selection.FontWeight = FontWeights.Bold;
            }
            else
            {
                RichTextBox.Selection.FontWeight = FontWeights.Normal;
            }
            
            this.ClosePopup();
        }

        private void btnItalic_Click(object sender, RoutedEventArgs e)
        {
            // italic
            if ((bool)btnItalic.IsChecked)
            {
                RichTextBox.Selection.FontStyle = FontStyles.Italic;
            }
            else
            {
                RichTextBox.Selection.FontStyle = FontStyles.Normal;
            }
            this.ClosePopup();
        }

        private void btnUnderline_Click(object sender, RoutedEventArgs e)
        {
            // underline
            if ((bool)btnUnderline.IsChecked)
            {
                RichTextBox.Selection.TextDecorations = C1TextDecorations.Underline;
            }
            else
            {
                RichTextBox.Selection.TextDecorations.Clear();
            }
            this.ClosePopup();
        }

        private void btnStrike_Click(object sender, RoutedEventArgs e)
        {
            // strikethrough
            if ((bool)btnStrike.IsChecked)
            {
                RichTextBox.Selection.TextDecorations = C1TextDecorations.Strikethrough;
            }
            else
            {
                RichTextBox.Selection.TextDecorations.Clear();
            }
            this.ClosePopup();
        }

        private void GrowFont(int size)
        {
            // grow font
            RichTextBox.Selection.TrimRuns();
            foreach (var run in RichTextBox.Selection.Runs)
            {
                run.FontSize += size;
            }
        }

        private void ShrinkFont(int size)
        {
            // shrink font
            RichTextBox.Selection.TrimRuns();
            foreach (var run in RichTextBox.Selection.Runs)
            {
                run.FontSize -= size;
            }
        }

        private void btnGrowFont_Click(object sender, RoutedEventArgs e)
        {
            // grow font
            this.GrowFont(2);
            this.ClosePopup();
        }

        private void btnShrinkFont_Click(object sender, RoutedEventArgs e)
        {
            // shrink font
            this.ShrinkFont(2);
            this.ClosePopup();
        }

        private void btnSubscript_Click(object sender, RoutedEventArgs e)
        {
            // subscript
            if ((bool)btnSubscript.IsChecked)
            {
                RichTextBox.Selection.InlineAlignment = C1VerticalAlignment.Sub;
                this.ShrinkFont(4);
            }
            else
            {
                RichTextBox.Selection.InlineAlignment = C1VerticalAlignment.Baseline;
                this.GrowFont(4);
            }
            this.ClosePopup();
        }

        private void btnSuperscript_Click(object sender, RoutedEventArgs e)
        {
            // superscript
            if ((bool)btnSuperscript.IsChecked)
            {
                RichTextBox.Selection.InlineAlignment = C1VerticalAlignment.Super;
                this.ShrinkFont(4);
            }
            else
            {
                RichTextBox.Selection.InlineAlignment = C1VerticalAlignment.Baseline;
                this.GrowFont(4);
            }
            this.ClosePopup();
        }

        #endregion      

        #region colors

        private void SetHighlightColor(SolidColorBrush color)
        {
            RichTextBox.Selection.InlineBackground = color;
        }

        private void SetFontColor(SolidColorBrush color)
        {
            RichTextBox.Selection.Foreground = color;
        }

        private void btnHighlight1_Click(object sender, RoutedEventArgs e)
        {
            // highlight 1
            if ((bool)btnHighlight1.IsChecked)
            {
                this.SetHighlightColor(_highlightColor1);
            }
            else
            {
                this.SetHighlightColor(null);
            }
            this.ClosePopup();
        }

        private void btnHighlight2_Click(object sender, RoutedEventArgs e)
        {
            // highlight 2
            if ((bool)btnHighlight2.IsChecked)
            {
                this.SetHighlightColor(_highlightColor2);
            }
            else
            {
                this.SetHighlightColor(null);
            }
            this.ClosePopup();
        }

        private void btnHighlight3_Click(object sender, RoutedEventArgs e)
        {
            // highlight 3
            if ((bool)btnHighlight3.IsChecked)
            {
                this.SetHighlightColor(_highlightColor3);
            }
            else
            {
                this.SetHighlightColor(null);
            }
            this.ClosePopup();
        }

        private void btnFontColor1_Click(object sender, RoutedEventArgs e)
        {
            // font color 1
            if ((bool)btnFontColor1.IsChecked)
            {
                this.SetFontColor(_fontColor1);
            }
            else
            {
                this.SetFontColor(null);
            }
            this.ClosePopup();
        }

        private void btnFontColor2_Click(object sender, RoutedEventArgs e)
        {
            // font color 2
            if ((bool)btnFontColor2.IsChecked)
            {
                this.SetFontColor(_fontColor2);
            }
            else
            {
                this.SetFontColor(null);
            }
            this.ClosePopup();
        }

        private void btnFontColor3_Click(object sender, RoutedEventArgs e)
        {
            // font color 3
            if ((bool)btnFontColor3.IsChecked)
            {
                this.SetFontColor(_fontColor3);
            }
            else
            {
                this.SetFontColor(null);
            }
            this.ClosePopup();
        }
        #endregion

        #region alignment

        private void AlignText(C1TextAlignment align)
        {
            RichTextBox.Selection.TextAlignment = align;
        }

        private void btnAlignLeft_Click(object sender, RoutedEventArgs e)
        {
            // align text left
            if ((bool)btnAlignLeft.IsChecked)
            {
                this.AlignText(C1TextAlignment.Left);
            }
            this.ClosePopup();
        }

        private void btnAlignCenter_Click(object sender, RoutedEventArgs e)
        {
            // align text center
            this.AlignText(C1TextAlignment.Center);
            this.ClosePopup();
        }

        private void btnAlignRight_Click(object sender, RoutedEventArgs e)
        {
            // align text right
            this.AlignText(C1TextAlignment.Right);
            this.ClosePopup();
        }

        private void btnJustified_Click(object sender, RoutedEventArgs e)
        {
            // justify text
            this.AlignText(C1TextAlignment.Justify);
            this.ClosePopup();
        }

        #endregion

        #region lists
        private void btnBullets_Click(object sender, RoutedEventArgs e)
        {
            // bullets
            if ((bool)btnBullets.IsChecked)
            {
                RichTextBox.Selection.MakeList(TextMarkerStyle.Disc);
            }
            else
            {
                RichTextBox.Selection.UndoList();
            }
            this.ClosePopup();
        }

        private void btnNumbering_Click(object sender, RoutedEventArgs e)
        {
            // bullets
            if ((bool)btnNumbering.IsChecked)
            {
                RichTextBox.Selection.MakeList(TextMarkerStyle.Decimal);
            }
            else
            {
                RichTextBox.Selection.UndoList();
            }
            this.ClosePopup();
        }
        #endregion

    }
}
